package Tset2;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * 双色球系统
 */
public class DoubleColorBall {
    private static final int RED_BALL_MIN = 1;
    private static final int RED_BALL_MAX = 33;
    private static final int BLUE_BALL_MIN = 1;
    private static final int BLUE_BALL_MAX = 16;
    private static final int RED_BALL_COUNT = 6;
    private static final int TOTAL_BALL_COUNT = RED_BALL_COUNT + 1;
    public static void main(String[] args) {
        System.out.println("双色球彩票系统");
        System.out.printf("请输入你的号码：（前%d位红色球&d~%d，第%d位蓝色球%d~%d）%n",
                RED_BALL_COUNT,RED_BALL_MIN,RED_BALL_MAX,RED_BALL_COUNT+1,BLUE_BALL_MIN,BLUE_BALL_MAX);
        int[] myNumbers = getPlayerNumber();
        int[] winningNumbers = generateWinningNumbers();
        System.out.println("中奖号码是："+Arrays.toString(winningNumbers));
        checkWinning(myNumbers,winningNumbers);
    }
    public static int[] getPlayerNumber() {
        Scanner sc = new Scanner(System.in);
        int[] numbers = new int[TOTAL_BALL_COUNT];
        for (int i = 0; i < TOTAL_BALL_COUNT; i++) {
            boolean isBuleBall = (i == RED_BALL_COUNT);
            String prompt = isBuleBall ?
                    String.format("请输入蓝色球(%d~%d)：",BLUE_BALL_MIN,BLUE_BALL_MAX) :
                    String.format("请输入第%d个红色球(%d~%d)：",i+1,RED_BALL_MIN,RED_BALL_MAX);
            System.out.println(prompt);
            while (true){
                try {
                    int num = Integer.parseInt(sc.nextLine());
                    if(isBuleBall){
                        if(num>=BLUE_BALL_MIN&&num<=BLUE_BALL_MAX){
                            numbers[i] = num;
                            break;
                        }
                    }else {
                        if(num>=RED_BALL_MIN&&num<=RED_BALL_MAX){
                            if(!contains(numbers,i,num)){
                                numbers[i] = num;
                                break;
                            }
                            System.out.println("红色球不能重复，请重新输入：");
                        }
                    }
                    System.out.println("输入无效，请重新输入：");
                }catch (NumberFormatException e){
                    System.out.println("请输入有效的数字：");
                }
            }
        }
    return numbers;
    }
    private static boolean contains(int[] array, int endIndex, int value) {
        for (int i = 0; i < endIndex; i++) {
            if (array[i] == value) {
                return true;
            }
        }
        return false;
    }

    public static int[] generateWinningNumbers(){
        int[] numbers = new int[TOTAL_BALL_COUNT];
        Random random = new Random();
        // 生成红色球（不重复）
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            int num;
            do {
                num = random.nextInt(RED_BALL_MAX) + 1;
            } while (contains(numbers, i, num));
            numbers[i] = num;
        }
        // 生成蓝色球
        numbers[RED_BALL_COUNT] = random.nextInt(BLUE_BALL_MAX) + 1;
        return numbers;
    }
    public static void checkWinning(int[] playerNumbers, int[] winningNumbers) {
        int redMatches = countRedMatches(playerNumbers, winningNumbers);
        boolean blueMatch = playerNumbers[RED_BALL_COUNT] == winningNumbers[RED_BALL_COUNT];

        System.out.printf("你中了%d个红球，%d个蓝球%n", redMatches, blueMatch ? 1 : 0);

        Prize prize = determinePrize(redMatches, blueMatch);
        if (prize != Prize.NONE) {
            System.out.println(prize.getMessage());
        } else {
            System.out.println("很遗憾，你没有中奖");
        }
    }
    private static int countRedMatches(int[] playerNumbers, int[] winningNumbers) {
        int count = 0;
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            for (int j = 0; j < RED_BALL_COUNT; j++) {
                if (playerNumbers[i] == winningNumbers[j]) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }
    private static Prize determinePrize(int redMatches, boolean blueMatch) {
        // 根据双色球官方规则确定奖项
        if (redMatches == 6 && blueMatch) {
            return Prize.FIRST_PRIZE;
        } else if (redMatches == 6) {
            return Prize.SECOND_PRIZE;
        } else if (redMatches == 5 && blueMatch) {
            return Prize.THIRD_PRIZE;
        } else if (redMatches == 5 || (redMatches == 4 && blueMatch)) {
            return Prize.FOURTH_PRIZE;
        } else if (redMatches == 4 || (redMatches == 3 && blueMatch)) {
            return Prize.FIFTH_PRIZE;
        } else if (blueMatch) {
            return Prize.SIXTH_PRIZE;
        }
        return Prize.NONE;
    }

    // 使用枚举定义奖项
    private enum Prize {
        FIRST_PRIZE("恭喜你获得一等奖！奖金1000万元"),
        SECOND_PRIZE("恭喜你获得二等奖！奖金500万元"),
        THIRD_PRIZE("恭喜你获得三等奖！奖金3000元"),
        FOURTH_PRIZE("恭喜你获得四等奖！奖金200元"),
        FIFTH_PRIZE("恭喜你获得五等奖！奖金10元"),
        SIXTH_PRIZE("恭喜你获得六等奖！奖金5元"),
        NONE("");

        private final String message;

        Prize(String message) {
            this.message = message;
        }
        public String getMessage() {
            return message;
        }
    }
}
